/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.genomemap.inputModel;

import genomemap.data.OrderElement;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;

/**
 * input model for the flex worker
 * @author Susanta Tewari
 * @version 1.0
 * @created 20-Nov-2007 7:38:26 PM
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class InputModel implements Serializable {

    /** Field description */
    private static final long serialVersionUID = 1L;

    /**
     * chromosome number
     */
    private int linkageGroup = -1;

    /**
     * oder set of markers
     */
    @OneToMany(cascade = CascadeType.PERSIST)
    private Collection<OrderElement> orderSet = new LinkedHashSet<>();

    /**
     * Holds value of property editable.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    /**
     * Constructs ...
     */
    public InputModel() {}

    @Override
    public int hashCode() {
        return new Integer(getLinkageGroup()).hashCode() + getOrderSet().hashCode();
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof InputModel) {

            InputModel workerInputModel = (InputModel) obj;

            return (getLinkageGroup() == workerInputModel.getLinkageGroup())
                   && getOrderSet().equals(workerInputModel.getOrderSet());
        }

        return false;
    }

    public void addOrder(OrderElement order) {
        getOrderSet().add(order);
    }

    public void addOrder(int val) {
        getOrderSet().add(new OrderElement(val));
    }

    /**
     * the maximum order
     */
    public int giveMaxOrder() {
        return new TreeSet<>(getOrderSet()).last().getVal();
    }

    /**
     * chromosome number
     */
    public int getLinkageGroup() {
        return linkageGroup;
    }

    /**
     * oder set of markers
     */
    public Collection<OrderElement> getOrderSet() {
        return orderSet;
    }

    /**
     * chromosome number
     *
     * @param newVal
     */
    public void setLinkageGroup(int newVal) {
        linkageGroup = newVal;
    }

    /**
     * oder set of markers
     *
     * @param newVal
     */
    void setOrderSet(Collection<OrderElement> newVal) {
        orderSet = newVal;
    }

    public int[] getOrder() {

        OrderElement[] array = getOrderSet().toArray(new OrderElement[0]);
        int[] order          = new int[array.length];

        for (int index = 0; index < order.length; index++) {

            order[index] = array[index].getVal();
        }

        return order;
    }

    public void setOrder(int[] order) {

        LinkedHashSet<OrderElement> newOrderSet = new LinkedHashSet<>();

        for (int val : order) {

            newOrderSet.add(new OrderElement(val));
        }

        setOrderSet(newOrderSet);
    }

    public int getOrderCount() {
        return getOrderSet().size();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
